// JavaScript Document 
//Author : Tham NN 
function validateEmail(email) {
    var atpos = email.indexOf("@");
    var dotpos = email.lastIndexOf(".");
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
        //  alert("Not a valid e-mail address");
        return false;
    }
    return true;
}
/**
 * check if users enter some invalid characters
 * 
 * @param {string} value
 * @returns {Boolean}
 */
function validText(value) {
    var chaos = new Array("'", "~", "#", "$", "%", "^", "&", "*", ";", "/", "\\", "|", "}", "{", "\"");
    var sum = chaos.length;
    for (var i in chaos) {
        if (!Array.prototype[i]) {
            sum += value.lastIndexOf(chaos[i])
        }
    }
    if (sum) {
        return false;
    }
    return true;
}
function checklogin()
{
    bool = 0;
    var user = document.getElementById('txt_email').value;
    user = user.trim();
    var password = document.getElementById('txt_password').value;
    password = password.trim();
    if (user == "") {
        document.getElementById('error_email').innerHTML = "Please fill in your User Name";
        bool = 1;
    }
    else if (validText(user) == false)
    {
        document.getElementById('error_email').innerHTML = "Please enter valid characters";
        bool = 1;
    }
    else {
        document.getElementById('error_email').innerHTML = "";
    }

    if (password == "") {
        document.getElementById('error_passw').innerHTML = "Please fill in your Password";
        bool = 1;
    }
    else {
        document.getElementById('error_passw').innerHTML = "";
    }
    if (bool == 1)
    {
        return false;
    }
    else
    {
        return true;
    }
}



//Select country
//Select country
// get log and get lat
function check_address2() {

    address = jQuery("#txt_postcode").val();

    address = address.trim();
    url_ajax = jQuery("#url_ajax").val();
    if (address != "")
    {
        $.post(url_ajax, {addresstest: address}, function(data) {


            // alert(data);
            // return false;
            data = data.trim();
            if (data != "")
            {

                jQuery("#ponit_add").val(data);
                jQuery("#check_add").val(1);
                jQuery('#error_postcode').html("<br clear='all' /><span style='color:rgb(61, 179, 99)'>Postcode is OK</spam>");
            }
            else
            {
                jQuery("#ponit_add").val(0);
                jQuery("#check_add").val(0);
                jQuery('#error_postcode').html("<br clear='all' />Generally indicates that the query (address or latlng) is missing");
            }
        });
    }
    else
    {
        jQuery("#ponit_add").val(0);
        jQuery("#check_add").val(0);
        jQuery('#error_postcode').html("<br clear='all' />");
    }
}

function check_address_updateprofile() {

    address = jQuery("#txt_location").val();
    txt_city = jQuery("#txt_city").val();
    state = jQuery("#state").val();
    address = address + " " + txt_city + " " + state;
    address = address.trim();
    url_ajax = jQuery("#url_ajax").val();
    if (address != "")
    {
        $.post(url_ajax, {addresstest: address}, function(data) {


            // alert(data);
            // return false;
            data = data.trim();
            if (data != "")
            {

                jQuery("#ponit_add").val(data);
                jQuery("#check_add").val(1);
                jQuery('#error_address').html("<br clear='all' /><span style='color:rgb(61, 179, 99)'>Address is OK</spam>");
            }
            else
            {
                jQuery("#ponit_add").val(0);
                jQuery("#check_add").val(0);
                jQuery('#error_address').html("<br clear='all' />Generally indicates that the query (address or latlng) is missing");
            }
        });
    }
    else
    {
        jQuery("#ponit_add").val(0);
        jQuery("#check_add").val(0);
        jQuery('#error_postcode').html("<br clear='all' />");
    }
}



function checkregister()
{

    url_ajax = jQuery("#url_ajax").val();
    name = document.getElementById('txt_name').value;
    name = name.trim();
    name2 = document.getElementById('txt_lastname').value;
    name2 = name2.trim();
    var email = document.getElementById('txt_email').value;
    email = email.trim();
    var password = document.getElementById('txt_password').value;
    password = password.trim();
    var repassword = document.getElementById('txt_repassword').value;
    repassword = repassword.trim();
    var username = document.getElementById('txt_username').value;
    username = username.trim();
    bool = 0;

    if (name == "") {
        document.getElementById('error_name').innerHTML = "Please fill in your First Name";

        bool = 1;
    }
    else {
        document.getElementById('error_name').innerHTML = "";
    }

    if (name2 == "") {
        document.getElementById('error_lastname').innerHTML = "Please fill in your Last Name";
        bool = 1;
    }
    else {
        document.getElementById('error_lastname').innerHTML = "";
    }
    if (email == "") {
        document.getElementById('error_email').innerHTML = "Please fill in your Email";
        bool = 1;
    }

    else if (validateEmail(email) == false) {
        document.getElementById('error_email').innerHTML = "Email is not a valid e-mail address";
        bool = 1;
    }
    else {

        $.post(url_ajax, {emailexist: email}, function(data) {

            if (data == 1)
            {
                document.getElementById('error_email').innerHTML = "Email have already exist";
                bool = 1;
            }
            else
            {
                document.getElementById('error_email').innerHTML = "";

            }
        });

    }
    if (password == "") {
        document.getElementById('error_passw').innerHTML = "Please fill in your Password";
        bool = 1;
    }
    else {
        document.getElementById('error_passw').innerHTML = "";
    }
    if (repassword == "") {
        document.getElementById('error_repassw').innerHTML = "Please fill in your Re Password";
        bool = 1;
    }
    else if (repassword != password) {
        document.getElementById('error_repassw').innerHTML = "Password and  Re Password is not the same";
        bool = 1;
    }
    else {
        document.getElementById('error_repassw').innerHTML = "";

    }


    var city = jQuery("#txt_city").val();
    city = city.trim();
    if (city == "") {

        jQuery("#error_city").html("Please fill in your city");
        bool = 1;
    } else {
        jQuery("#error_city").html("");
    }



    var state = jQuery("#state").val();
    if (state == "") {

        jQuery("#error_state").html("Please fill in your state");
        bool = 1;
    }
    else {
        jQuery("#error_state").html("");
    }
    var postcode = jQuery("#txt_postcode").val();
    postcode = postcode.trim();
    if (postcode == "") {
        jQuery("#error_postcode").html("Please fill in your Postcode");
        bool = 1;
    }
    else {
        jQuery("#error_postcode").html("");
    }

//    var securityCode = jQuery("#security_code").val();
//    if (securityCode == "")
//    {
//        jQuery("#error_capcha").html("Please you enter security characters");
//        bool = 1;
//    }
//    else
//    {
//        jQuery("#error_capcha").html("");
//    }
    if (bool == 1)
    {
        return false;
    }
    else
    {
        return true;
    }


}
function checkusername2()
{
    url_ajax = jQuery("#url_ajax").val();
    user = jQuery("#txt_username2").val();
    //	alert(user);
    user = user.trim();
    if (user == "")
    {
        document.getElementById('error_username').innerHTML = "Please fill in your User Name";

    }
    else if (validText(user) == false)
    {
        document.getElementById('error_username').innerHTML = "User Name is not special character";
    }
    else
    {
        $.post(url_ajax, {username2: user}, function(data) {
            //alert(data);

            if (data == 1)
            {
                document.getElementById('error_username').innerHTML = "Username have already exist";
            }
            else
            {
                document.getElementById('error_username').innerHTML = "";
            }
            //alert(data);
        });

    }
}
function checkupdateprofile()
{

    var bool = 0;

    name = document.getElementById('txt_name').value;
    name = name.trim();
    name2 = document.getElementById('txt_lastname').value;
    name2 = name2.trim();
    if (name == "") {
        document.getElementById('error_name').innerHTML = "Please fill in your First Name";

        bool = 1;
    }
    else {
        document.getElementById('error_name').innerHTML = "";
    }
    if (name2 == "") {
        document.getElementById('error_lastname').innerHTML = "Please fill in your Last Name";
        bool = 1;
    }
    else {
        document.getElementById('error_lastname').innerHTML = "";
    }

    error_email = jQuery('#error_email').html();
    if (error_email != "") {
        bool = 1;
    }

    // check password 
    var password = document.getElementById('txt_password').value;
    password = password.trim();
    var repassword = document.getElementById('txt_repassword').value;
    repassword = repassword.trim();

    if (repassword != password) {
        document.getElementById('error_repassw').innerHTML = "Password and  Re Password is not the same";
        bool = 1;
    }
    else {
        document.getElementById('error_repassw').innerHTML = "";

    }



    var city = jQuery("#txt_city").val();
    if (city == "") {

        jQuery("#error_city").html("Please fill in your city");
        bool = 1;
    } else {
        jQuery("#error_city").html("");
    }



    var state = jQuery("#state").val();

    if (state == "") {

        jQuery("#error_state").html("Please fill in your state");
        bool = 1;
    }
    else {
        jQuery("#error_state").html("");
    }


    var postcode = jQuery("#txt_postcode").val();
    postcode = postcode.trim();
    if (postcode == "") {
        jQuery("#error_postcode").html("Please fill in your Postcode");
        bool = 1;
    }
    else {
        jQuery("#error_postcode").html("");
    }
    if (bool == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}


/*Forgot Password */
function forgetpass()
{
    site_url = jQuery("#site_url").val();
    site_url = site_url + '/forgot-password';
    //console.log(site_url);
    window.location.href = site_url;
    return false;
}
function check_fogetpass()
{
    //username =  jQuery('#txt_name').val();
    //username =  username.trim();
    email = jQuery('#txt_email').val();
    email = email.trim();
    bool = 0; //  determine 

    if (email == "") {
        document.getElementById('error_email').innerHTML = "Email can not be blank";
        bool = 1;
    }
    else if (validateEmail(email) == false) {
        document.getElementById('error_email').innerHTML = "Email is not a valid e-mail address";
        bool = 1;
    }
    else {


        document.getElementById('error_email').innerHTML = "";
    }

    //alert(username);
    if (bool == 1) {
        return false;
    }
    else {
        return true;
    }
}


jQuery(document).ready(function($) {

    jQuery("#logout_user").click(function() {

        url_ajax = jQuery("#url_ajax").val();
        site_url = jQuery("#site_url").val();
        $.post(url_ajax, {logout: 1}, function(data) {
            window.location.href = site_url;
        });
    });
});




